<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form
  name="formLogging"
  ng-submit="loggingCtrl.update(loggingCtrl.api)"
  novalidate
  class="api-analytics-logging-configuration"
  ng-class="{'api-analytics-logging-configuration_enabled' : loggingCtrl.enabled}"
>
  <div class="gv-forms" layout="column">
    <div class="gv-forms-header">
      <h1>Logging configuration</h1>
      <a ui-sref="management.apis.detail.analytics.logs">Back to logs</a>
    </div>

    <div class="gv-form">
      <div class="gv-form-content">
        <div layout="row" flex-xs="50">
          <md-input-container class="md-block" flex-gt-sm>
            <gv-switch
              label="Enabled / Disabled"
              description="Use logging with caution and only for development purpose.<br/> It can have an impact on API performances and is requiring more spaces for analytics storage."
              gv-model
              ng-on-gv-switch:input="loggingCtrl.switchMode($event)"
              ng-model="loggingCtrl.enabled"
            >
            </gv-switch>
          </md-input-container>
        </div>

        <div class="api-analytics-logging-configuration_form-details">
          <div class="gv-page-draft-banner" ng-if="loggingCtrl.maxDuration && loggingCtrl.maxDuration > 0">
            <ng-md-icon icon="warning" class="gv-warning"></ng-md-icon>
            A maximum duration of <code>{{loggingCtrl.maxDuration}}</code>milliseconds will be applied to your condition.
          </div>

          <div layout="row" flex-xs="50">
            <md-input-container class="md-block" flex-gt-sm>
              <label class="md-no-float">Mode</label>

              <gv-option ng-attr-options="{{loggingModes}}" ng-model="loggingCtrl.api.proxy.logging.mode" gv-model></gv-option>
            </md-input-container>
          </div>

          <div layout="row" flex-xs="50">
            <md-input-container class="md-block" flex-gt-sm>
              <label class="md-no-float">Content</label>
              <gv-option ng-attr-options="{{contentModes}}" ng-model="loggingCtrl.api.proxy.logging.content" gv-model></gv-option>
            </md-input-container>
          </div>

          <div layout="row" flex-xs="50">
            <md-input-container class="md-block" flex-gt-sm>
              <label class="md-no-float">Scope</label>
              <gv-option ng-attr-options="{{scopeModes}}" ng-model="loggingCtrl.api.proxy.logging.scope" gv-model></gv-option>
            </md-input-container>
          </div>

          <div layout="row" flex-xs="50">
            <md-input-container class="md-block" flex-gt-sm>
              <label class="md-no-float">Condition</label>
              <div layout="row" flex-xs="50">
                <gv-expression-language
                  rows="1"
                  gv-model
                  ng-attr-grammar="{{loggingCtrl.spelGrammar}}"
                  ng-model="loggingCtrl.api.proxy.logging.condition"
                  placeholder="{#request.headers['Content-Type'][0] === 'application/json'}"
                ></gv-expression-language>
                <gv-button
                  ng-if="!loggingCtrl.hasCondition()"
                  outlined
                  link
                  icon="design:edit"
                  title="Show editor"
                  ng-click="loggingCtrl.showConditionEditor()"
                >
                </gv-button>
                <gv-button
                  outlined
                  link
                  ng-if="loggingCtrl.hasCondition()"
                  icon="home:trash"
                  title="Clear condition"
                  ng-click="loggingCtrl.clearCondition()"
                >
                </gv-button>
              </div>
            </md-input-container>
          </div>
        </div>

        <div class="md-actions gravitee-api-save-button" layout="row">
          <gv-button
            permission
            permission-only="'api-log-u'"
            type="submit"
            icon="general:save"
            ng-disabled="formLogging.$invalid || formLogging.$pristine"
          >
            Save
          </gv-button>
          <gv-button
            permission
            permission-only="'api-log-u'"
            outlined
            type="button"
            icon="general:update"
            ng-click="loggingCtrl.reset()"
            ng-disabled="formLogging.$pristine"
          >
            Reset
          </gv-button>
        </div>
      </div>
    </div>
  </div>
</form>
